import { Injectable } from '@nestjs/common';
import { spider1 } from '../util/spider';
import { getStorage } from '../util/storage';
import { getHotNewsList } from '../util/common';

@Injectable()
export class NewsService {
    async getBaiduNews() {
        const cach = getStorage('baidu');
        if (cach) {
            return cach;
        }

        const html = await spider1('http://news.baidu.com/', false);
        return getHotNewsList(html, 'baidu', '#pane-news .bold-item a');
    }

    async getTencentNews() {
        const cach = getStorage('tencent');
        if (cach) {
            return cach;
        }

        const html = await spider1('http://qq.com/', true);
        return getHotNewsList(html, 'tencent', '#tab-news-01 .yw-list a');
    }

    async getNews163() {
        const cach = getStorage('news163');
        if (cach) {
            return cach;
        }

        const html = await spider1('http://news.163.com/', true);
        return getHotNewsList(html, 'news163', '.main_center_news .mod_top_news2 a');
    }
}

